from django import forms
from app_user.models import UserInfo


class RegisterForm(forms.Form):
    username = forms.CharField(label='用户名', min_length=5, max_length=12,
                               widget=forms.TextInput(attrs={
                                   "class": "form-control",
                                   "placeholder": "请输入用户名",
                               }),
                               error_messages={
                                   'min_length': '用户名最少5位',
                                   'max_length': '用户名最多12位'
                               })
    password = forms.CharField(label='密码', min_length=5, max_length=24,
                               widget=forms.PasswordInput(attrs={
                                   "class": "form-control",
                                   "placeholder": "请输入密码",
                               }),
                               error_messages={
                                   'required': '密码不能为空',
                                   'min_length': '密码最少5位',
                                   'max_length': '密码最多24位'
                               })
    confirm_pwd = forms.CharField(label='确认密码', min_length=5, max_length=24,
                                  widget=forms.PasswordInput(attrs={
                                      "class": "form-control",
                                      "placeholder": "请再次输入密码",
                                  }),
                                  error_messages={
                                      'required': '密码不能为空',
                                      'min_length': '密码最少5位',
                                      'max_length': '密码最多24位'
                                  })
    email = forms.EmailField(label='邮箱',
                             widget=forms.TextInput(attrs={
                                 "class": "form-control",
                                 "placeholder": "请输入邮箱",
                             }),
                             error_messages={
                                 'invalid': '邮箱格式错误'
                             })
    blog_title = forms.CharField(label='站点标题', min_length=5, max_length=12,
                                 widget=forms.TextInput(attrs={
                                     "class": "form-control",
                                     "placeholder": "请输入站点标题",
                                 }),
                                 error_messages={
                                     'min_length': '标题最少5位',
                                     'max_length': '标题最多12位'
                                 })

    def clean_username(self):
        # 针对用户名的局部钩子
        username = self.cleaned_data.get('username')
        is_exists = UserInfo.objects.filter(username=username).exists()
        if is_exists:
            self.add_error(field='username', error='用户已存在，不可以重复注册')
        return username

    def clean(self):
        # 针对密码的全局钩子
        pwd = self.cleaned_data.get('password')
        confirm_pwd = self.cleaned_data.get('confirm_pwd')
        if pwd != confirm_pwd:
            self.add_error(field='confirm_pwd', error='密码不一致')
        return self.cleaned_data


class LoginForm(forms.Form):
    username = forms.CharField(label='用户名', min_length=5, max_length=12,
                               widget=forms.TextInput(attrs={
                                   "class": "form-control",
                                   "placeholder": "请输入用户名",
                               }),
                               error_messages={
                                   'min_length': '用户名最少5位',
                                   'max_length': '用户名最多12位'
                               })
    password = forms.CharField(label='密码', min_length=5, max_length=24,
                               widget=forms.PasswordInput(attrs={
                                   "class": "form-control",
                                   "placeholder": "请输入密码",
                               }),
                               error_messages={
                                   'required': '密码不能为空',
                                   'min_length': '密码最少5位',
                                   'max_length': '密码最多24位'
                               })
    confirm_pwd = forms.CharField(label='确认密码', min_length=5, max_length=24,
                                  widget=forms.PasswordInput(attrs={
                                      "class": "form-control",
                                      "placeholder": "请再次输入密码",
                                  }),
                                  error_messages={
                                      'required': '密码不能为空',
                                      'min_length': '密码最少5位',
                                      'max_length': '密码最多24位'
                                  })

    def clean_username(self):
        # 针对用户名的局部钩子
        username = self.cleaned_data.get('username')
        is_exists = UserInfo.objects.filter(username=username).exists()
        if not is_exists:
            self.add_error(field='username', error='用户不存在，请先注册')
        return username

    def clean(self):
        # 针对密码的全局钩子
        pwd = self.cleaned_data.get('password')
        confirm_pwd = self.cleaned_data.get('confirm_pwd')
        if pwd != confirm_pwd:
            self.add_error(field='confirm_pwd', error='密码不一致')
        return self.cleaned_data
